package com.bc.web.controller.admin;

import java.util.List;

import com.bc.web.model.Dictionary;
import com.fs.common.extend.FSController;
import com.fs.common.extend.FSSearchCondition;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

public class DictionaryController extends FSController{
	
	public void search(){
		List<Dictionary> parentList = Dictionary.dao.selectParent();
		setAttr("parentList", parentList);
		render("basedata/dictionaryList.html");
	}
	
	public void searchChild() {
		FSSearchCondition sc = getSearchCondition();
		List<Dictionary> items = Dictionary.dao.selectChild(sc);
		setAttr("items", items);
		render("basedata/dictionary.html");
	}
	
	public void addPage(){
		String parentCode = getPara("parentCode");
		if(!("0").equals(parentCode)) {
			Dictionary parent = Dictionary.dao.selectByKey(parentCode);
			setAttr("parent", parent);
		}
		setAttr("parentCode", parentCode);
		render("basedata/dictionaryAdd.html");
	}
	
	public void updatePage(){
		Dictionary dictionary = Dictionary.dao.findById(getPara("id"));
		setAttr("dictionary", dictionary);
		if(!dictionary.getParentCode().equals("0")) {
			setAttr("parent", Dictionary.dao.selectByKey(dictionary.getParentCode()));
		}
		render("basedata/dictionaryUpdate.html");
	}
	
	public void add(){
		Dictionary dictionary = getModel(Dictionary.class);
		if(StrKit.isBlank(dictionary.getParentCode())) {
			dictionary.setParentCode("0");
		}
		Dictionary temp = Dictionary.dao.selectByKey(dictionary.getCode());
		if(temp != null) {
			renderFail("编码已存在,不允许重复");
		}else {
			dictionary.save();
			renderSuccess();
		}
	}
	
	public void update(){
		Dictionary dictionary = getModel(Dictionary.class);
		dictionary.update();
		renderSuccess();
	}
	
	@Before(Tx.class)
	public void delete(){
		Dictionary dictionary = Dictionary.dao.findById(getPara("id"));
		if(dictionary.getParentCode().equals("0")) {
			Dictionary.dao.deleteByParentCode(dictionary.getCode());
		}
		dictionary.delete();
		renderSuccess(false);
	}
	
}
